package com.brilliance.project.webapp.action.sysmanage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.brilliance.project.common.PropertyConstants;
import com.brilliance.project.common.tools.StringUtil;
import com.brilliance.project.service.IBaseService;
import com.brilliance.project.service.sysmanage.IDepartService;
import com.brilliance.project.webapp.action.BaseAction;
import com.brilliance.project.webapp.form.BaseForm;
import com.brilliance.project.webapp.form.PropertyForm;
import com.brilliance.project.webapp.form.sysmanage.AgentForm;
import com.brilliance.project.webapp.form.sysmanage.DepartForm;

@Scope("prototype")
@Component("agentAction")
public class AgentAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	// 存放查询的条件
	private AgentForm agentForm = new AgentForm();
	// 存放agentId的key值
	private String agentId;
	private String[] agentIds;
	private String[] data;
	// 存放查询条件
	private String agentName;
	// 查询的结果集
	private List<AgentForm> queryList = new ArrayList<AgentForm>();
	// 下拉选单-部门
	private List<DepartForm> departDropdownList = new ArrayList<DepartForm>();
	// radiobox-状态
	private List<PropertyForm> agentFlagList = new ArrayList<PropertyForm>();

	@Autowired
	private IBaseService agentServiceImpl = null;
	@Autowired
	private IDepartService departServiceImpl = null;

	private String resultStr;
	private JSONObject result;// 返回的json

	/**
	 * 查询
	 * 
	 * @return
	 */
	public String query() {
		Object pageObj = this.getHttpServletRequest().getParameter("page");

		int page = 1;
		if (null != pageObj) {
			page = Integer.parseInt(pageObj.toString());
		}
		int row = Integer.parseInt(this.getHttpServletRequest().getParameter(
				"rows"));// 接受参数page和rows
		agentForm.setCurPageNo(page + "");
		agentForm.setCountPerPage(row);
		agentForm.setAgent_id(agentId);
		agentForm.setAgent_name(agentName);
		List<BaseForm> list = agentServiceImpl.queryByPg(agentForm, log);
		baseForm2agentFormList(list);
		this.setTotal(Integer.parseInt(agentForm.getTotalRecord()));

		AgentForm agentForm = null;
		Map<String, Object> map = null;
		rows.clear();
		for (int i = 0; i < queryList.size(); i++) {
			agentForm = queryList.get(i); // 最原始循环方法到到student对象
			map = new HashMap<String, Object>();
			map.put("agent_id", agentForm.getAgent_id()); // 以键值对的形式保存到map中
			map.put("agent_name", agentForm.getAgent_name());
			map.put("depart_name", agentForm.getDepart_name());
			map.put("agent_flagDsp", agentForm.getAgent_flagDsp());
			rows.add(map); // 循环保存map到list对象this.rows中
		}

		return JSON;
	}

	/**
	 * 转到新增画面agentAdd.jsp
	 * 
	 * @return
	 */
	public String toAdd() {
		agentForm.setAgent_flagDsp("0");
		return "toAdd";
	}

	// 进入修改画面
	public String toModify() {
		AgentForm af = new AgentForm();
		af.setAgent_id(agentId);
		baseForm2agentFormList(agentServiceImpl.queryByPg(af, log));
		if (queryList.size() == 1) {
			agentForm = queryList.get(0);
		}
		return "toModify";
	}

	/**
	 * 检查是否有重复ID
	 * 
	 * @return
	 */
	public String checkAgentId() {
		Object obj = getHttpServletRequest().getAttribute("agentId");
		String agentId = null;
		if (null != obj) {
			agentId = (String) obj;
		}
		if (StringUtil.isNull(agentId)) {
			resultStr = "请输入用户ID";
			return JSON;
		}
		AgentForm agentForm = new AgentForm();
		agentForm.setAgent_id(agentId);
		List<BaseForm> list = agentServiceImpl.query(agentForm, log);
		if (list.size() > 0) {
			resultStr = "用户ID：" + agentId + "已经存在！";
		}
		return JSON;
	}

	/**
	 * 存储一笔资料
	 * 
	 * @return
	 */
	public String save() {
		AgentForm queryAF = new AgentForm();
		queryAF.setAgent_id(agentForm.getAgent_id());
		List<BaseForm> list = agentServiceImpl.query(queryAF, log);
		if (list.size() > 0) {
			this.setPopMsg("用户ID重复");
		} else {
			if (agentServiceImpl.insert(agentForm, log)) {
				this.setIfCloseWindow(true);
			} else {
				this.setPopMsg("新增失败。");
			}
		}
		return "toAdd";
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String modify() {
		if (agentServiceImpl.update(agentForm, log)) {
			this.setIfCloseWindow(true);
		} else {
			this.setPopMsg(this.getText("popmsg.del.failure"));
		}
		return "toModify";
	}

	/**
	 * 删除一笔资料
	 * 
	 * @return
	 */
	public String delete() {
		AgentForm af = new AgentForm();
		af.setAgent_id(agentId);
		try {
			if (agentServiceImpl.delete(af, log)) {
				resultStr = this.getText("popmsg.del.success");
				return JSON;
			} else {
				resultStr = this.getText("popmsg.del.failure");
				return JSON;
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultStr = e.getMessage();
			return JSON;
		}

	}

	private void baseForm2agentFormList(List<BaseForm> list) {
		AgentForm af = null;
		queryList.clear();
		for (BaseForm bf : list) {
			af = (AgentForm) bf;
			queryList.add(af);
		}
	}

	// ==============setter & getter=================

	public AgentForm getAgentForm() {
		return agentForm;
	}

	public void setAgentForm(AgentForm agentForm) {
		this.agentForm = agentForm;
	}

	public List<AgentForm> getQueryList() {
		return queryList;
	}

	public void setQueryList(List<AgentForm> queryList) {
		this.queryList = queryList;
	}

	public String execute() {
		return SUCCESS;
	}

	public List<DepartForm> getDepartDropdownList() {
		return departServiceImpl.query(new DepartForm());
	}

	public void setDepartDropdownList(List<DepartForm> departDropdownList) {
		this.departDropdownList = departDropdownList;
	}

	public List<PropertyForm> getAgentFlagList() {
		return PropertyConstants.getAgentFlag();
	}

	public void setAgentFlagList(List<PropertyForm> agentFlagList) {
		this.agentFlagList = agentFlagList;
	}

	public String getResultStr() {
		return resultStr;
	}

	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}

	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public String getAgentId() {
		return agentId;
	}

	public void setAgentId(String agentId) {
		this.agentId = agentId;
	}

	@Override
	public List<Object> getRows() {
		return rows;
	}

	@Override
	public int getTotal() {
		return super.getTotal();
	}

	public String getAgentName() {
		return agentName;
	}

	public void setAgentName(String agentName) {
		this.agentName = agentName;
	}

	public String[] getAgentIds() {
		return agentIds;
	}

	public void setAgentIds(String[] agentIds) {
		this.agentIds = agentIds;
	}

	public String[] getData() {
		return data;
	}

	public void setData(String[] data) {
		this.data = data;
	}

}
